﻿using System.Collections;
using System.Diagnostics.Eventing.Reader;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;

namespace ClassicPassWd
{
    /// <summary>
    /// MultiPassword.xaml 的交互逻辑
    /// </summary>
    public partial class MultiPassword : UserControl
    {
        private char[] alphaCharsUpper =
        {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z'
        };

        private char[] alphaCharsLower =
        {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z'
        };

        public MultiPassword()
        {
            InitializeComponent();
        }

        private void BoxProclaim_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            int length = BoxProclaim.Text.Replace(" ", "").Length;
            ;
            LocaleCombo.Items.Clear();
            if (length != 0)
            {
                var list = GetKSpace(26);

                foreach (var t in list)
                {
                    LocaleCombo.Items.Add(t);
                }

                LocaleCombo.SelectedIndex = 0;
                GetPassWord((int) LocaleCombo.SelectedValue);
            }
        }

        private void GetPassWord(int offset)
        {
            string str1 = BoxProclaim.Text.Replace(" ", "");
            StringBuilder builder = new StringBuilder();
            char[] strChars = str1.ToCharArray();
            for (int i = 0; i < str1.Length; i++)
            {
                if (strChars[i] >= 'A' && strChars[i] <= 'Z')
                    builder.Append(alphaCharsUpper[(strChars[i] - 65) * offset % 26]);
            }

            BoxPassword.Text = builder.ToString();
        }

        private ArrayList GetKSpace(int num)
        {
            ArrayList list = new ArrayList(num) {1};
            for (int i = 2; i <= num; i++)
                if (Prime(num, i))
                    list.Add(i);
            return list;
        }

        private void LocaleCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BoxPassword.Text = "";

            if (LocaleCombo.SelectedValue != null)
                GetPassWord((int) LocaleCombo.SelectedValue);
        }


        private static bool Prime(int n, int m)
        {
            int t = 0;
            while (m > 0)
            {
                t = n % m;
                n = m;
                m = t;
            }

            if (n == 1) return true;
            return false;
        }

        private void BoxProclaim_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Space)
            {
                e.Handled = false;
                return;
            }

            e.Handled = new Regex("[^A-Za-z]").IsMatch(GetCharFromKey.KeyChars(e.Key).ToString());
        }
    }
}